<?php
namespace app\wechat\controller;
use think\Config;
use think\Controller;
use app\models\Bases;
class Base extends Controller{
    /**
     * ajax请求返回成功信息
     * @param  string $data
     * @return array
     */
    public static function formatSuccessResult($data = null){
        return self::formatResult(0, '操作成功', $data);
    }
    /**
     * ajax请求返回错误信息
     * @param int $code
     * @param string $errorMsg
     * @param string $data
     * @return array
     */
    public static function formatResult($code, $errorMsg, $data = null){
        return array('code' => $code,'msg' => $errorMsg,'data'=>$data);
    }

    /**
     * @param $where
     * @return array|false|mixed|\PDOStatement|string|\think\Model
     * 根据条件查询用户信息
     */
    public static function getUserInfo($where,$field){
        $user = new Bases('user');
        $userInfo = $user->find($where,$field);
        if(empty($userInfo)){
            throw new \LogicException('该用户不存在',10011);
        }
        return $userInfo;
    }

    /**
     * @param $where
     * @param $field
     * @return array
     */
    public static function _getUserInfo($where,$field){
        $user = new Bases('user');
        $user_info = $user->find($where,$field);
        if(empty($user_info)){
            $result = ['bind_status' => Config::get('wechat.is_bind')];
            cookie('bind_status',Config::get('wechat.is_bind'),time() + 3600 *15);
        }
        if(!empty($user_info)  &&  !empty($user_info['openid'])){
            throw new \LogicException('该手机号码已经被使用了',10025);
        }
        if(!empty($user_info) && empty($user_info['openid'])){
            $result = ['bind_status' => Config::get('wechat.is_not_bind')];
            cookie('bind_status',Config::get('wechat.is_not_bind'),time() + 3600 *15);
        }
        return $result;
    }
    /**
     * @param $code
     * @param $action
     * @param $username
     * @return bool
     * 验证手机验证码
     */
    public static function checkCode($code,$action,$username){
        $reCode = cookie($username.$action);
        if($code!==$reCode){
            throw new \LogicException('手机验证码不正确',10014);
        }
        return true;
    }
}